package directory_sync;

import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.SwingWorker;
import javax.swing.border.Border;

import org.apache.log4j.Logger;

public class DisplayProgress extends JFrame implements PropertyChangeListener
{
	private final int FRAME_WIDTH = 600, FRAME_HEIGTH = 170;
	static final int MINIMUM = 0, MAXIMUM = 100;
	
	int progress;
	
	private JFrame frame;
	private JProgressBar progressBar;
	private JLabel statusLbl;
	private JButton cancelBtu;
	private Logger logger;
	private Task task;
	
	public DisplayProgress(Logger aLogger)
	{	
		logger = aLogger;
		frame = new JFrame("Processing deep synchronisation");
		Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
		int xFramePos = (int)(dim.getWidth()-FRAME_WIDTH)/2;
		int yFramePos = (int)(dim.getHeight()-FRAME_HEIGTH)/2;
		frame.setSize(FRAME_WIDTH, FRAME_HEIGTH);
		frame.setLocation(xFramePos ,yFramePos);
		frame.setResizable(false);
		frame.setAlwaysOnTop(true);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		Container contentPane = frame.getContentPane();
		contentPane.setLayout(new GridLayout(3, 1));
		progressBar = new JProgressBar(MINIMUM, MAXIMUM);
		progressBar.setStringPainted(true);
		Border barBorder = BorderFactory.createTitledBorder("In progress...");
		progressBar.setBorder(barBorder);
		contentPane.add(progressBar);
		contentPane.add(getStatusPanel());
		contentPane.add(getButtonPanel());
		
		task = new Task();
        task.addPropertyChangeListener(this);
        task.execute();
		cancelBtu.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				task.cancel(0);
			}
		});
		
		frame.setVisible(true);
	}
	
	private JPanel getStatusPanel()
	{
		JPanel panel = new JPanel();
		panel.setLayout(new FlowLayout());
		statusLbl = new JLabel("");
		panel.add(statusLbl);
		return panel;
	}
	
	private JPanel getButtonPanel()
	{
		JPanel panel = new JPanel();
		panel.setLayout(new FlowLayout());
		cancelBtu = new JButton("Cancel");
		panel.add(cancelBtu);
		return panel;
	}
	
    class Task extends SwingWorker<Void, Void>
    {
        //main task, executed in background thread
        public Void doInBackground()
        {
            progress = 0;
            setProgress(0);
            while(progress < 100)
            {
                try
                {
                    //sleep for up to one second
                    Thread.sleep(100);
                }
                catch(InterruptedException ex)
                {
    				logger.error(ex);
    				ex.printStackTrace();
                }
                setProgress(progress);
            }
            return null;
        }

        public void cancel(Object exit)
        {
			System.exit(0);
		}

		//executed in event dispatching thread
        public void done()
        {
            System.out.println("sync done");
            progressBar.setValue(progress);
        	statusLbl.setText("Status: sync completed");
            try
            {
				Thread.sleep(2000);
			}
            catch(InterruptedException ex)
			{
				logger.error(ex);
				ex.printStackTrace();
			}
    		frame.setVisible(false);
        }
    }

    public void propertyChange(PropertyChangeEvent evt)
    {
        if ("progress" == evt.getPropertyName())
        {
            int progress = (Integer) evt.getNewValue();
            progressBar.setValue(progress);
        } 
    }

    public void setProgress(int aProgress, String aPath)
    {
    	progress = aProgress;
    	statusLbl.setText("Processing object: " + new File(aPath).getName());
    }
}